package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.zm.cs.domain.ZMCSInitialData;

/**隶属于初始数据模块：初始数据管理
 * <p>Title: ServiceZMCSInitialData.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 路涛
 * @version 1.0
 * <pre>History:
 *          2010-12-17 上午09:38:09  路涛  Create
 *</pre>
 */
public class ServiceZMCS30 extends ServiceEPBase {
	
	/**
	 * 初始化列表数据方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-24下午08:44:45 严洪浩
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		inInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.dataType", new String[] { "标准版", "项目版" }, new String[] { "标准版", "项目版" }, false));// 数据类型
		inInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.type", new String[] { "系统", "用户" }, new String[] { "系统", "用户" }, false));// 系统类型
		inInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.status", new String[] { "停用", "在用" }, new String[] { "停用", "在用" }, false));// 状态
		String runType = FrameworkInfo.getProjectType();
		inInfo.set("runType", runType);// 运行状态
		inInfo.set("fdCreatorId", UserSession.getUserId());// 登录人的默认值
		inInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		return inInfo;
	}
	
	/**
	 * 主表数据查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 上午09:38:09  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCSInitialData.query", new ZMCSInitialData());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.dataType", new String[] { "标准版", "项目版" }, new String[] { "标准版", "项目版" }, false));// 数据类型
		String runType = FrameworkInfo.getProjectType();
		outInfo.set("runType", runType);// 运行状态
		outInfo.set("fdCreatorId", UserSession.getUserId());// 登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		return outInfo;
	}
	
	/**
	 * 主表数据新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2010-12-17 上午09:38:09  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCSInitialData _dom = new ZMCSInitialData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			boolean validate = validate(inInfo, _dom, "ZMCSInitialData.validate", params);
			if(validate) {
				return inInfo;
			}
			else {
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_INITIALDATA"));//主键
				this.dao.insert("ZMCSInitialData.insert",_dom);
			}
		}
		out = this.query(inInfo);
		changeInitialData(out);//如果修改成功则重新刷新数据
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 主表数据修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 上午09:38:09  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCSInitialData _dom = new ZMCSInitialData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = validate(inInfo, _dom, "ZMCSInitialData.validate", params);
			if(validate) {
				return inInfo;
			}
			else {
				_dom.setFdCreatorId( UserSession.getUserId().toString());//默认为当前登录用户
				this.dao.update("ZMCSInitialData.update",_dom);
			}
		}
		out = this.query(inInfo);
		changeInitialData(out);//如果修改成功则重新刷新数据
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 主表数据删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 上午09:38:09  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		super.delete(inInfo, "ZMCSInitialDataDetails.super_delete");
		EiInfo _temp = super.delete(inInfo, "ZMCSInitialData.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        
        changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
		
	/**
	 * 校验序号、编号、名称不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 上午09:38:09  路涛  Create
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCSInitialData _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdName = _dom.getFdName();
		String fdNo = _dom.getFdNo();
		String fdSerialNumber = _dom.getFdSerialNumber().toString();
		
		params.put("fdName", fdName);
		params.put("fdNo", fdNo);
		params.put("fdSerialNumber", fdSerialNumber);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSInitialData _temp = (ZMCSInitialData)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdSerialNumber() != null && String.valueOf(_temp.getFdSerialNumber()).equals(fdSerialNumber)) {
				msg.append(" 序号 ");
			}
			msg.append("重复  (注：可能与其它模块的业务设置冲突)！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	
	}
	
	/**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:59:02 严洪浩
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.INITIALDATA_STATUS = 1;
		}
	}
}
